//
//  FileRequest.swift
//  Employee
//
//  Created by swordjoy on 2017/4/12.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON
import Moya

struct FileRequest: DataResultProtocol {
  
  static let shared = FileRequest()
  
  func uploadImages(datas: [Data], thumbSquareSide: String = "500", progressClosure: @escaping ( (Progress) -> () ) , success: @escaping ( ([ImageIdModel]) -> () ) ) -> Cancellable {
    
    let requestKey = SJApp.functionKey()
    if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
    
    let cancle = FileProvider.request(FileAPI.uploadImages(images: datas, thumbSquareSide: thumbSquareSide), queue: nil, progress: { (progress) in
      
      if let temp = progress.progressObject {
        progressClosure(temp)
      }
      
    }) { (result) in
      
      GlobalConfig.requestCache.removeValue(forKey: requestKey)
      
      self.dataProtocol(with: result, success: { (data) in
        
        let models = ImageIdModel.array(from: data.arrayValue)
        success(models)
        
      }, overdue: { 
        
        let _ = FileRequest.shared.uploadImages(datas: datas, thumbSquareSide: thumbSquareSide, progressClosure: progressClosure, success: success)
        
      }, failure: nil)
      
    }
    
    
    GlobalConfig.requestCache[requestKey] = cancle
    
    return cancle
    
  }
    func newUploadImages(datas: [Data], thumbSquareSide: String = "500", progressClosure: @escaping ( (Progress) -> () ) , success: @escaping ( (Array<Any>) -> () ) ) -> Cancellable {
        
        let requestKey = SJApp.functionKey()
        if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }
        
        let cancle = FileProvider.request(FileAPI.uploadImages(images: datas, thumbSquareSide: thumbSquareSide), queue: nil, progress: { (progress) in
            
            if let temp = progress.progressObject {
                progressClosure(temp)
            }
            
        }) { (result) in
            
            GlobalConfig.requestCache.removeValue(forKey: requestKey)
            
            self.dataProtocol(with: result, success: { (data) in
                
                var array: Array<Any> = []
                for json in data.arrayValue {
                    array.append(json.dictionaryObject ?? ["":""])
                }
                
                success(array)
                
            }, overdue: {
                
                let _ = FileRequest.shared.newUploadImages(datas: datas, thumbSquareSide: thumbSquareSide, progressClosure: progressClosure, success: success)
                
            }, failure: nil)
            
        }
        
        
        GlobalConfig.requestCache[requestKey] = cancle
        
        return cancle
        
    }
    func uploadHeadIcon(imageData: Data, thumbSquareSide: String = "500", success: @escaping (ImageIdModel) -> (), failure: @escaping VoidClosure ) -> Cancellable  {
  
        let requestKey = SJApp.functionKey()
        if let cacheCancle = GlobalConfig.requestCache[requestKey] { return cacheCancle }

        let cancle = FileProvider.request(FileAPI.uploadHeadIconImage(image: imageData, thumbSquareSide: thumbSquareSide)) { (result) in
          
          GlobalConfig.requestCache.removeValue(forKey: requestKey)
          
          self.dataProtocol(with: result, success: { (data) in
            
            let model = ImageIdModel.init(json: data)
            success(model)
            
          }, overdue: { 
            
            let _ = FileRequest.shared.uploadHeadIcon(imageData: imageData, thumbSquareSide: thumbSquareSide, success: success, failure: failure)
            
          }, failure: failure)
         
        }
        
        GlobalConfig.requestCache[requestKey] = cancle
        return cancle
    
  }
  
}
